// CS 564:: PROJECT STAGE 5 - Catalog
//
// Caela Northey (caela) 905 653 2238
// Colin Laska   (laska) 906 150 7258

#include "catalog.h"

// Destroys a relation. It performs the following steps:
//
// 	removes the catalog entry for the relation
// 	destroys the heap file containing the tuples in the relation
//
// Returns:
// 	OK on success
// 	error code otherwise
//
const Status RelCatalog::destroyRel(const string & relation)
{
  Status status;

  if (relation.empty() || 
      relation == string(RELCATNAME) || 
      relation == string(ATTRCATNAME))
    return BADCATPARM;

  //remove single entry from RelCat
  status = relCat->removeInfo(relation);
  if (status != OK){
   return status;
  }

  //remove attributes for this relation
  status = attrCat->dropRelation(relation);
  if (status != OK){
   return status;
  }

  //destroy heapfile
  status = db.destroyFile(relation);
  if (status != OK){
   return status;  
  }

  return status; //should be OK by here
}

//
// Drops a relation. It performs the following steps:
//
// 	removes the catalog entries for the relation
//
// Returns:
// 	OK on success
// 	error code otherwise
//
const Status AttrCatalog::dropRelation(const string & relation)
{
  Status status;
  AttrDesc *attrs;
  int attrCnt, i;

  if (relation.empty()) return BADCATPARM;

  //remove multiple entries from AttrCat
    status = attrCat->getRelInfo(relation.c_str(), i, attrs);

  for(int j = 0; j < i; j++){

    status = attrCat->removeInfo(relation.c_str(), attrs[j].attrName);
    if (status != OK){
      return status;
    }

  } //end for

  return status; //should be OK by here
}


